<!-- 购物车组件 -->
<template>
  <div class="shopcart">
    <div class="cart-list-wrap" v-if="cartData.length > 0">
      <header class="header-address">
        <div class="address-box">
          <i class="iconfont icon-icon_Location"></i>
          <span
            v-text="
              cartData[0].province +
              ' ' +
              cartData[0].city +
              ' ' +
              cartData[0].county
            "
          ></span>
        </div>
        <div class="h-edit color-red">编辑商品</div>
      </header>
      <div class="" v-for="(cart, index) in cartData" :key="index">
        <div class="card-shop-head">
          <div class="checkbox_icon">
            <i class="iconfont icon-xuanzhong"></i>
            <!-- <i class="iconfont icon-xuanzhong2"></i> -->
          </div>
          <div class="checkbox_tit">
            <strong>
              <a href="javascript:;" class="" v-text="cart.shop_name"></a>
            </strong>
            <i class="iconfont icon-fanhui2"></i>
          </div>
        </div>
        <div class="checkbox_con">
          <div class="checkbox_icon" @click="changeSelect(cart, index)">
            <i class="iconfont icon-xuanzhong" v-if="!cart.isSelect"></i>
            <i class="iconfont icon-xuanzhong2" v-else style="color: red"></i>
          </div>
          <div class="cart-goods-info">
            <div class="img-box">
              <img :src="cart.goods_thumb" class="img" />
            </div>
            <div class="card_content">
              <div class="card_left">
                <div class="goods-title">
                  <a href="javascript:;" v-text="cart.goods_name"></a>
                </div>
                <div class="goods-info">
                  <div class="currency-price">
                    <em>¥</em>
                    <span v-text="cart.shop_price"></span>
                  </div>
                  <div class="oper">
                    <div class="stepper">
                      <button
                        class="subtract"
                        @click="jianCartNum(index)"
                      ></button>
                      <input type="number" v-model="cart.value" />
                      <button
                        class="increase"
                        @click="addCartNum(index)"
                      ></button>
                    </div>
                    <div class="oper-icon">
                      <i class="iconfont icon-fabulous"></i>
                      <i
                        class="iconfont icon-delete"
                        @click="deleteCart(index)"
                      ></i>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 购物车空显示 -->
    <div v-else class="nocart">
      <i class="iconfont icon-gouwuche"></i>
      <p>您的购物车还是空空的，快去逛逛吧！</p>
      <div class="card-btn">
        <button class="van-button">
          <router-link to="/home">去逛逛</router-link>
        </button>
        <button class="van-button">
          <router-link to="/home">看看关注</router-link>
        </button>
      </div>
    </div>
    <h5 class="title-hrbg">
      <span>猜你喜欢</span>
      <hr />
    </h5>
    <!-- 购物车底部 -->
    <div class="cart-footer" v-if="cartData.length > 0">
      <div class="selectAll" @click="changeSelectAll">
        <i class="iconfont icon-xuanzhong" v-if="!selectAll"></i>
        <i class="iconfont icon-xuanzhong2" style="color: red" v-else></i>
      </div>
      <div class="totalPrice">
        合计：
        <span v-text="'¥' + totalPrice"></span>
      </div>
      <div class="gobuy" v-text="'去结算' + '(' + countCart + ')'"></div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {};
  },
  //监听属性 类似于data概念
  computed: {
    cartData() {
      return this.$store.state.cartDatas;
    },
    selectAll() {
      let selectStatus = this.$store.state.cartDatas.every((item) => {
        return item.isSelect == true;
      });
      return selectStatus;
    },
    totalPrice() {
      let priceAll = 0;
      this.$store.state.cartDatas.map((item) => {
        if (item.isSelect) {
          priceAll += item.value * item.shop_price;
        }
      });
      return priceAll;
    },
    countCart() {
      let count = 0;
      this.$store.state.cartDatas.map((item) => {
        if (item.isSelect) {
          count += item.value;
        }
      });
      return count;
    },
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    changeSelect(cart, index) {
      //   console.log(cart);
      //   console.log(index);
      cart.isSelect = !cart.isSelect;
      this.$store.commit("changeSelectStatus", { cart: cart, index: index });
    },
    changeSelectAll() {
      this.$store.commit("changeSelectAll");
    },
    addCartNum(index) {
      this.$store.commit("addCartNum", index);
    },
    jianCartNum(index) {
      this.$store.commit("jianCartNum", index);
    },
    deleteCart(index) {
      this.$store.commit("deleteCart", index);
    },
  },
};
</script>
<style lang="less">
em {
  font-style: normal;
}
.shopcart {
  .header-address {
    padding: 1.1rem;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .address-box {
      display: flex;
      align-items: center;
      width: 80%;
      color: #777;
      height: 1.9rem;
      line-height: 1.9rem;
      position: relative;
      .icon-icon-Location {
        position: absolute;
        font-size: 1.6rem;
        top: 1px;
        left: 0;
      }
    }
    .color-red {
      color: #f92028;
    }
  }
  .card-shop-head {
    padding: 1.1rem;
    display: flex;
    align-items: center;
    .checkbox_icon {
      display: flex;
      align-items: center;
      .iconfont {
        font-size: 1.3rem;
      }
    }
    .checkbox_tit {
      display: flex;
      align-items: center;
      width: 100%;
      padding-left: 1rem;
      box-sizing: border-box;
      strong {
        font-size: 1.3rem;
      }
      .icon-fanhui2 {
        font-size: 1.2rem;
        margin-left: 5px;
      }
    }
  }
  .checkbox_con {
    display: flex;
    align-items: center;
    width: 100%;
    padding-left: 1rem;
    box-sizing: border-box;
    .checkbox_icon {
      .iconfont {
        font-size: 1.3rem;
      }
    }
    .cart-goods-info {
      display: flex;
      width: 100%;
      margin: 0 1rem 0 1rem;
      box-sizing: border-box;
      background: #ffffff;
      border-radius: 0.5rem 0.5rem 0 0;
      padding: 1.5rem 0 1.5rem 1.5rem;
      position: relative;
      &::after {
        content: " ";
        position: absolute;
        left: 1rem;
        bottom: -1px;
        right: 1rem;
        height: 1px;
        z-index: 5;
        border-bottom: 1px solid #e5e5e5;
        color: #e5e5e5;
        transform-origin: 0 0;
        transform: scaleY(0.5);
      }
      .img-box {
        width: 7rem;
        height: 7rem;
        img {
          height: 100%;
        }
      }
      .card_content {
        width: 100%;
        padding: 0 1rem;
        .card_left {
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .goods-title {
            font-size: 1.3rem;
            overflow: hidden;
            -webkit-line-clamp: 2;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
          }
          .goods-info {
            display: flex;
            justify-content: space-between;
            .currency-price {
              font-size: 15px;
              font-weight: 700;
              display: inline-block;
              color: rgb(242, 14, 40);
            }
            .oper {
              display: flex;
              align-items: center;
              .stepper {
                display: flex;
                align-items: center;
                button {
                  width: 2rem;
                  height: 2rem;
                  border: 1px solid #eee;
                  outline: none;
                  background: none;
                  position: relative;
                  &::before {
                    content: "";
                    width: 9px;
                    height: 1px;
                    position: absolute;
                    margin: auto;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: #666;
                  }
                }
                .subtract {
                  border-radius: 5px 0 0 5px;
                }
                .increase {
                  border-radius: 0 5px 5px 0;
                  &::after {
                    width: 1px;
                    height: 9px;
                    content: "";
                    position: absolute;
                    margin: auto;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: #666;
                  }
                }
                input {
                  width: 2.5rem;
                  height: 2rem;
                  box-sizing: border-box;
                  padding: 0;
                  outline: none;
                  font-size: 1.2rem;
                  border: 1px solid #eee;
                  text-align: center;
                }
              }
              .oper-icon {
                margin-left: 1rem;
                .icon-fabulous {
                  font-size: 1.7rem;
                  margin-right: 0.3rem;
                }
              }
            }
          }
        }
      }
    }
  }
  .nocart {
    margin: 1.1rem;
    background-color: #fff;
    border-radius: 0.5rem;
    overflow: hidden;
    text-align: center;
    padding-bottom: 4rem;
    i {
      font-size: 5rem;
      width: 10rem;
      height: 10rem;
      line-height: 10rem;
      color: #ccc;
      border: 1px solid #efefef;
      background-color: #fbfbfb;
      border-radius: 50%;
      display: block;
      margin: 4rem auto 0;
    }
    p {
      font-size: 1.3rem;
      margin-top: 1.5rem;
    }
    .card-btn {
      margin-top: 1.5rem;
      .van-button {
        height: 3rem;
        margin-right: 0.5rem;
        padding: 0 15px;
        background-color: #fff;
        border-radius: 2px;
        color: #333;
        border: 1px solid #eee;
      }
    }
  }
  .title-hrbg {
    font-size: 1.3rem;
    position: relative;
    z-index: 1;
    height: 4rem;
    line-height: 4rem;
    overflow: hidden;
    color: #888;
    text-align: center;
    span {
      background: #f3f3f3;
      padding: 1rem 0.6rem;
      font-size: 1.4rem;
      font-weight: 500;
      z-index: 10;
    }
    hr {
      background: #ddd;
      height: 1px;
      border: 0;
      position: absolute;
      left: 100px;
      right: 100px;
      top: 50%;
      margin-top: 1px;
      z-index: -1;
    }
  }
  .cart-footer {
    position: fixed;
    background: #fff;
    bottom: 5rem;
    height: 4.4rem;
    border-top: 1px solid #efefef;
    width: 100%;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.4rem;
    .selectAll {
      width: 10%;
      text-align: center;
      .iconfont {
        font-size: 1.3rem;
      }
    }
    .totalPrice {
      width: 65%;
      text-align: right;
      span {
        font-size: 16px;
        font-weight: 700;
        display: inline-block;
        color: rgb(242, 14, 40);
        margin-right: 2rem;
      }
    }
    .gobuy {
      width: 25%;
      line-height: 4.4rem;
      font-size: 14px;
      background: #f44;
      color: #fff;
      text-align: center;
    }
  }
}
</style>